package com.lbs.jiepiaisi.utils;

import android.widget.Toast;

import com.lbs.jiepiaisi.manager.App;
import com.lbs.jiepiaisi.view.WaitingDialogControll;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 时间：2017/8/9 17:55
 * 作用：okHttp封装类
 */

public class HttpUtil {
    private static HttpUtil httpUtil;
    private OkHttpClient okHttpClient;
    /**
     * 获得单例
     */
    public static HttpUtil getHttpUtil(){
        if(httpUtil == null){
            httpUtil = new HttpUtil();
        }
        return httpUtil;
    }
    //构造方法
    private HttpUtil() {
        /**
         * 设置超时时间及缓存：
         * okhttp只会对get请求进行缓存，post请求是不会进行缓存
         */
        OkHttpClient.Builder builder = null;
        File sdcache = App.instance.getExternalCacheDir();
        int cacheSize = 10 * 1024 * 1024;
        try {
            builder = new OkHttpClient.Builder()
                    .connectTimeout(15, TimeUnit.SECONDS)
                    .writeTimeout(20, TimeUnit.SECONDS)
                    .readTimeout(20, TimeUnit.SECONDS)
                    .cache(new Cache(sdcache.getAbsoluteFile(), cacheSize));
        }catch (NullPointerException e){
            builder = new OkHttpClient.Builder()
                    .connectTimeout(15, TimeUnit.SECONDS)
                    .writeTimeout(20, TimeUnit.SECONDS)
                    .readTimeout(20, TimeUnit.SECONDS);
        }finally {
            if(builder != null){
                okHttpClient = builder.build();
            }
        }
    }
    /**
     * Post请求
     */
    public void post(final String url, FormBody formBody, final Listener listener){
        if(!NetUtil.isNetworkAvailable(App.instance)){
            WaitingDialogControll.dismissWaitingDialog();
            Toast.makeText(App.instance,"请检查网络连接",Toast.LENGTH_SHORT).show();
            return;
        }
        Request request = new Request.Builder()
                .post(formBody)
                .url(url)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                    //无网络
                    listener.noNetwork();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){//联网成功
                    listener.success(response);
                }else{                      //联网失败
                    listener.failed();
                }
            }
        });
    }

    public interface Listener{
        /**
         * 联网成功
         */
        void success(Response response);
        /**
         * 联网失败
         */
        void failed();
        /**
         * 无网络
         */
        void noNetwork();
    }


}
